﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Ovvio.Data;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
using Ovvio.Business;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using Ovvio.Web.Core;
using System.Data;
using System.Web.Mvc;

namespace Ovvio.Web.Areas.Admin.Models
{
    public class ProductModel : BaseModel<Product>
    {
        private HttpPostedFileBase _image;

        [ScaffoldColumn(false)]
        public int Id { get { return Entity.Id; } set { Entity.Id = value; } }

        [Required]
        public string Name { get { return Entity.Name; } set { Entity.Name = value; } }

        public string Details { get { return Entity.Details; } set { Entity.Details = value; } }

        [Required]
        public decimal Price { get { return (Entity.Price.HasValue ? Entity.Price.Value : (decimal)0); } set { Entity.Price = value; } }
        
        [DisplayName("On-Stock")]
        public bool IsOnStock { get { return Entity.IsOnStock; } set { Entity.IsOnStock = value; } }


        [DisplayName("Published")]
        public bool IsPublished { get { return Entity.IsPublished; } set { Entity.IsPublished = value; } }

        [DisplayName("Image")]
        public HttpPostedFileBase PostImage { get { return _image; }
            set
            {
                _image = value;
                
            }
        }

        [DisplayName("Type")]
        public int ItemTypeId
        {
            get { return Entity.ItemType == null ? -1 : Entity.ItemType.Id; }

            set { Entity.ItemTypeReference.EntityKey = new EntityKey("OvvioDataContext.ItemTypes", "Id", value); }
        }

        public SelectList ItemTypes
        {
            get
            {
                return CreateList<ItemType, OvvioDataContext>("Id", "Description", p => p.Id == ItemTypeId);
            }
        }

        [DisplayName("Category")]
        public int CategoryId
        {
            get { return Entity.ProductCategory == null ? -1 : Entity.ProductCategory.Id; }

            set { Entity.ProductCategoryReference.EntityKey = new EntityKey("OvvioDataContext.ProductCategories", "Id", value); }
        }

        public SelectList ProductCategories
        {
            get
            {
                return CreateList<ProductCategory, OvvioDataContext>("Id", "Category", p => p.Id == CategoryId);
            }
        }

        [DisplayName("Discount")]
        public int DiscountId
        {
            get { return Entity.Discount == null ? -1 : Entity.Discount.Id; }

            set { Entity.DiscountReference.EntityKey = new EntityKey("OvvioDataContext.Discounts", "Id", value); }
        }

        public SelectList Discounts
        {
            get
            {
                return CreateList<Discount, OvvioDataContext>("Id", "Description", p => p.Id == DiscountId);
            }
        }

        [DisplayName("Rating")]
        public int RatingId
        {
            get { return Entity.ProductRating == null ? -1 : Entity.ProductRating.Id; }

            set { 
                Entity.ProductRatingReference.EntityKey = new EntityKey("OvvioDataContext.ProductRatings", "Id", value); }
        }

        public SelectList Ratings
        {
            get
            {
                return CreateList<ProductRating, OvvioDataContext>("Id", "Description", p => p.Id == RatingId);
            }
        }

        [DisplayName("Promo")]
        public int PromoId
        {
            get { return Entity.Promo == null ? -1 : Entity.Promo.Id; }

            set { Entity.PromoReference.EntityKey = new EntityKey("OvvioDataContext.Promos", "Id", value); }
        }

        public SelectList Promos
        {
            get
            {
                return CreateList<Promo, OvvioDataContext>("Id", "Description", p => p.Id == PromoId);
            }
        }
    }
}
